// customerPackage/pages/visit/visit.js

var bmap = require('../../../lib/bmap-wx'); //bmap-wx.js文件存放路径
var BMap = new bmap.BMapWX({
  ak: 'd2KjPsoWTSGWv83Nif3XD7CuRCLxCrFr' //百度地图密钥
});
var startTimer = null;

const app = getApp()

var times1 = null
Page({

  /**
   * 页面的初始数据
   */
  data: {
    path: app.globalData.path,
    customer: '',
    show: false, //拜访成功弹窗
    status: false, //是否在范围内
    customer: '',
    customerVisit: {
      visitNote: '',
      visitPosition: '正在获取定位中...',
      visitLong: '',
      visitLat: '',
    },
    hour: '00',
    min: '00',
    flag: false, // false  未打卡   true  已打卡
    isColon: true,
    length: 0, // 距离目的地
    visitTime: '',
    notePop: false,
  },


  // 拜访打卡
  visit() {

    if (this.data.flag) {
      return
    }

    if (!this.data.status) {
      wx.showToast({
        title: '当前不在范围内',
        icon: 'none'
      })
      return
    }
    this.addCusVisit();

    console.log(this.data.customer);
    console.log(this.data.customerVisit);




  },
  addCusVisit() {
    var url = app.globalData.url + "customer/addVisit",
      that = this;
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        customerId: that.data.customer.customerId,
        visitLat: that.data.customerVisit.visitLat,
        visitLong: that.data.customerVisit.visitLong,
        visitPosition: that.data.customerVisit.visitPosition,
        visitNote: that.data.customerVisit.visitNote,
        userId: app.globalData.userId,
        companyId: app.globalData.companyId,

      }, res => {
        console.log(res);
        if (res.flag) {
          console.log(res);
          wx.hideLoading()

          var str = that.data.hour + ':' + that.data.min
          that.setData({
            visitTime: str,
            flag: true,
            show: true
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '打卡失败，请重试',
            showCancel: false,
            complete: (res) => {
              if (res.confirm) {

              }
            }
          })
        }
      },
      err => {
        console.log(err);
      })

  },

  getNowDate() {
    var myDate = new Date();
    var hours = myDate.getHours();
    var mins = myDate.getMinutes();
    var secs = myDate.getSeconds();
    var min = '',
      hour = '';
    min = (mins >= 10 ? mins : '0' + mins);
    hour = hours >= 10 ? hours : '0' + hours;
    var that = this;
    times1 = setInterval(function () {
      secs++;
      if (secs == 60) {
        secs = 0;
        mins++;
        min = (mins >= 10 ? mins : '0' + mins);
      }
      if (mins == 60) {
        mins = 0;
        hour++;
        min = (mins >= 10 ? mins : '0' + mins);
        hour = hours >= 10 ? hours : '0' + hours;
      }

      that.setData({
        min,
        hour
      })
    }, 1000)

    this.setData({
      min,
      hour
    })
  },



  getMyInfo() {
    var that = this,
      customer = this.data.customer,
      status = this.data.status,
      length = 0,
      customerVisit = this.data.customerVisit;

    console.log(1)
    wx.showLoading({
      title: '定位中...',
    })
    BMap.regeocoding({
      success(data) {
        console.log(data);
        customerVisit.visitLat = data.originalData.result.location.lat
        customerVisit.visitLong = data.originalData.result.location.lng
        customerVisit.visitPosition = data.originalData.result.formatted_address
        var juliNum = that.juli(customer.customerLat, customer.customerLong, customerVisit.visitLat, customerVisit.visitLong)
        console.log(juliNum);
        status = juliNum > 500 ? false : true
        that.setData({
          customerVisit,
          status,
          length: juliNum
        })
        wx.hideLoading()
      },
      fail(res) {
        wx.showToast({
          title: '定位失败,下拉刷新',
          icon: 'none'
        })
        console.log(res);
        wx.hideLoading()
      }
    })
  },


  //计算两点之间的距离
  juli(lat1, lng1, lat2, lng2) {
    var radLat1 = lat1 * Math.PI / 180.0;
    var radLat2 = lat2 * Math.PI / 180.0;
    var a = radLat1 - radLat2;
    var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
    s = s * 6378.137;
    s = Math.round(s * 10000) / 10000;
    return s * 1000
  },


  closeSus() {
    this.setData({
      show: false
    })
  },


  // 输入框编辑
  inputHandle(e) {
    var str = e.currentTarget.dataset.flag
    var val = e.detail.value
    this.setData({
      [str]: val
    })
    console.log(this.data);
  },

  onClose() {
    this.setData({
      notePop: false
    })
  },

  showNote() {
    this.setData({
      notePop: true
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    console.log(options);
    this.setData({
      customer: JSON.parse(options.customer)
    })

    this.getNowDate()
    console.log(this.data.customer);
    this.getMyInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})